package com.qianfeng.controller;

import com.qianfeng.dto.Message;
import com.qianfeng.dto.Page;
import com.qianfeng.dto.Result;
import com.qianfeng.entity.Role;
import com.qianfeng.enums.CommonEnum;
import com.qianfeng.exceptions.CommonException;
import com.qianfeng.service.RoleService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * 添加
     *
     * @param role
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/add_role", method = RequestMethod.POST)
    public Result addRole(Role role) {
        try {
//            int i=1/0;
            roleService.addRole(role);
            Result result = new Result();
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.ADD_ROLE_ERROR);
        }
    }

    /**
     * 删除
     *
     * @param roleId
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/delete_role")
    public Result deleteRole(Integer roleId) {
        try {
//            int i=1/0;
            roleService.deleteRole(roleId);
            Result result = new Result();
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.DELETE_ROLE_ERROR);
        }
    }

    /**
     * 更新
     *
     * @param role
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/update_role", method = RequestMethod.POST)
    public Result updateRole(Role role) {
        try {
//            int i=1/0;
            roleService.updateRole(role);
            Result result = new Result();
            Message message = new Message();
            result.setMessage(message);
            return result;
        } catch (Exception e) {
            throw new CommonException(CommonEnum.UPDATE_ROLE_ERROR);
        }
    }

    /**
     * 分页
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getRolePage")
    public Result getRolePage(Integer pageNo, Integer pageSize) {


        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 2;
        }
        Page rolePage = roleService.getRolePage(pageNo, pageSize);
        Result result = new Result();
        Message message = new Message();
        message.setMsg("成功");
        result.setMessage(message);
        result.setObj(rolePage);
        return result;

    }

    /**
     * 通过id查询
     *
     * @param roleId
     * @return
     */
    @RequiresRoles(value = "manager")
    @CrossOrigin
    @ResponseBody
    @RequestMapping(value = "/getRoleByRoleId")
    public Result getRoleByRoleId(Integer roleId) {

        Role roleByRoleId = roleService.getRoleByRoleId(roleId);
        Result result = new Result();
        Message message = new Message();
        result.setMessage(message);
        result.setObj(roleByRoleId);
        return result;

    }


}
